package com.samp.bblock.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

public class ListUtils {
	
	/**
	 * 将一个List平分成splitNum个小的List.
	 * @param orginList
	 * @param splitNum
	 * @return
	 */
	public static <T> List<List<T>> splitList(List<T> orginList, Integer splitNum){
		if( orginList == null || orginList.size() == 0 
				|| splitNum == null || splitNum < 2){
			return null;
		}
		List<List<T>> result = new ArrayList<List<T>>();
		int remaider = orginList.size()%splitNum;
		int number = orginList.size()/splitNum;
		int offset = 0 ;
		for( int i = 0 ; i < splitNum ; i++ ){
			List<T> value = null;
			if( remaider > 0 ){
				value = orginList.subList(i*number+offset,(i+1)*number+offset+1);
				remaider--;
				offset++;
			}else{
				value = orginList.subList(i*number+offset,(i+1)*number+offset);
			}
			result.add(value);
		}
		return result;
	}
	
	public static void removeRepeatItem(List<String> list){
		if( list == null || list.size() == 0 ){
			return;
		}
		HashSet<String> hashSet = new HashSet<String>(list);
		list.clear();
		list.addAll(hashSet);
	}
	
	public static void main(String[] args) {
		List<String> list  = new ArrayList<String>();
		for( int i = 0 ; i < 185; i ++ ){
			list.add(String.valueOf(i));
		}
		//int n = list.size()%100==0?list.size()/100:(list.size()/100)+1;
		List<List<String>> result = splitList(list,18);
		System.out.println(result.toString());
		String[] a = new String[]{"张三","李四","张三","王五","王麻子","李四","王五"};
		List<String> repeatList  = new ArrayList<>();
		for( String s : a){
			repeatList.add(s);
		}
		System.out.println(repeatList);
		removeRepeatItem(repeatList);
		System.out.println(repeatList);
	}
}
